var app = getApp(),
  a = app.requirejs("core");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.id || !options.type){
      a.jump('',5);
    }
    this.setData(options)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },

  /**
   * 提交退款
   */
  formSubmit: function (e) {
    var that = this,
    i=that.data.type;
    var formData = e.detail.value;
    if (formData.info == '') {
      a.alert('内容不能为空！');
      return;
    }
    if(i==1){
      that.refund(formData.info);
    }else if(i==2){
      that.refund2(formData.info);
    } else if (i == 3) {
      that.refund3(formData.info);
    } else if (i == 4) {
      that.refund4(formData.info);
    }
  },
  /**
   *退款
   * @param order_id 
   * @return string
   */
  refund: function (apply_refund) {
    var that = this,
      order_id = that.data.id;
      a.get("order/RefundOrder", {
        order_id: order_id,
        apply_refund: apply_refund,
        buyer_id: getApp().getCache("userinfo").uid,
      }, function (t) {
        if (t.code == 0) {
          a.get("Wxpush/UniformPushMsg", {
            order_no: order_id,
            uid: getApp().getCache("userinfo").uid,
            order_type: 1,
            send_type: 2,
          }, function (tt) {

          })
          a.success('申请成功');
          setTimeout(function () {
            a.jump('/yb_mingpian/pages/order/index',5)
          }, 1000)
        } else {
          a.alert(t.msg)
        }

        // 0 == t.code ? (
        //   a.success('申请成功')
        // ) : (a.alert(t.msg))
      });

  },
  refund2: function (i) {
    var that = this,
      order_id = that.data.id;
      a.get("Bargain/RefundOrder", {
        order_id: order_id,
        apply_refund: i,
        buyer_id: getApp().getCache("userinfo").uid
      }, function (t) {

        if (t.code == 0) {
          a.get("Wxpush/UniformPushMsg", {
            order_no: order_id,
            uid: getApp().getCache("userinfo").uid,
            order_type: 4,
            send_type: 2,
          }, function (tt) {

          })
          a.success('申请成功');
          setTimeout(function () {
            a.jump('/yb_mingpian/pages/order/index',5)
          }, 1000) 
        } else {
          a.alert(t.msg)
        }

        // 0 == t.code ? (
        //   a.success('申请成功')
        // ) : a.alert(t.msg)
      });
  },
  refund3: function (e) {
    var that = this,
      order_id = that.data.id;
          a.get('Pintuan/refundOrder', {
            id: order_id,
            apply_refund: e,
            uid: getApp().getCache('userinfo').uid
          }, function (t) {
            if (t.code == 0) {
              a.get("Wxpush/UniformPushMsg", {
                order_no: order_id,
                uid: getApp().getCache("userinfo").uid,
                order_type: 3,
                send_type: 2,
              }, function (tt) {

              })
              a.success('申请成功');
              a.jump('/yb_mingpian/pages/order/index', 5)
            } else {
              a.alert(t.msg)
            }
    })
  },
  refund4: function (n) {
    var that = this,
      order_id = that.data.id;
      a.get("Miaosha/RefundOrder", {
        order_id: order_id,
        apply_refund: n,//退款内容
        buyer_id: getApp().getCache("userinfo").uid
      }, function (t) {

        if (t.code == 0) {
          a.get("Wxpush/UniformPushMsg", {
            order_no: order_id,
            uid: getApp().getCache("userinfo").uid,
            order_type: 2,
            send_type: 2,
          }, function (tt) {

          })
          a.success('申请成功');
          setTimeout(function () {
            a.jump('/yb_mingpian/pages/order/index', 5)
          }, 1000)
        } else {
          a.alert(t.msg)
        }

        // 0 == t.code ? (
        //   a.success('申请成功')
        // ) : (a.alert(t.msg))
      })
  },
})